//
//  PersonInfo.m
//  ArchivingObjs
//
//  Created by Wang Shao-chuan on 2010/12/28.
//  Copyright 2010 National Taiwan University. All rights reserved.
//

#import "PersonInfo.h"


@implementation PersonInfo

- (id)init
{
    self = [super init];
    if (self) {
        [self clear];
    }
    return self;
}

-(NSString *) nickname
{
	if (mNickename != nil)
		return [[[NSString alloc] initWithString: mNickename] autorelease];
	else
		return nil;
}
-(void) setNickname: (NSString *) newName
{
	if (mNickename) {
		if ([mNickename isEqualToString: newName])
			return;
		[mNickename release];
	}
	mNickename = [newName copy];
}
-(NSDate *) date;
{
	if (mBirthdate != nil)
		return [[mBirthdate copy] autorelease];
	else
		return nil;
}
-(void) setDate: (NSDate *) newDate;
{
	if (mBirthdate) {
		if ([mBirthdate isEqualToDate: newDate])
			return;
		[mBirthdate release];
	}
	mBirthdate = [newDate copy];
}
-(BOOL) isMale
{
	return mIsMale;
}
-(void) setIsMale: (BOOL) isMale
{
	mIsMale = isMale;
}
-(unsigned int) age
{
	return mAge;
}	
-(void) setAge: (unsigned int) newAge
{
	mAge = newAge;
}
-(void) dealloc
{
	[mBirthdate release];
	[mNickename release];
	[super dealloc];
}
-(id)copyWithZone:(NSZone *)zone
{
	PersonInfo *newInfo = [[[self class] allocWithZone: zone] init];
	if (newInfo) {
		[newInfo setNickname: [self nickname]];
		[newInfo setDate: [self date]];
		[newInfo setAge: [self age]];
		[newInfo setIsMale: [self isMale]];
	}
	return newInfo;
}
-(void)encodeWithCoder: (NSCoder *) encoder 
{
	[encoder encodeObject: mNickename forKey: @"PersonInfoNickname"];
	[encoder encodeObject: mBirthdate forKey: @"PersonInfoDate"];
	[encoder encodeInt: mAge forKey: @"PersonInfoAge"];
	[encoder encodeBool: mIsMale forKey:@"PersonInfoIsMale"];
}

-(id)initWithCoder: (NSCoder *) decoder 
{
	mNickename = [[decoder decodeObjectForKey: @"PersonInfoNickname"] retain];
	mBirthdate = [[decoder decodeObjectForKey: @"PersonInfoDate"] retain];
	mAge = [decoder decodeIntForKey:@"PersonInfoAge"];
	mIsMale = [decoder decodeBoolForKey:@"PersonIfnoIsMale"];
		
	return self;
}

- (void)clear
{
	if (mNickename)
		[mNickename release];
	mNickename = [NSString string];
	if (mBirthdate)
		[mBirthdate release];
	mBirthdate = [[NSDate dateWithTimeIntervalSinceReferenceDate:0] retain];
	mAge = 1;
	mIsMale = YES;
}


@end
